/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pb138.db;

import java.util.ArrayList;
import java.util.List;
import javax.xml.transform.OutputKeys;

import org.exist.storage.serializers.EXistOutputKeys;
import org.exist.xmldb.EXistResource;
import org.exist.xmldb.XQueryService;
import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Collection;
import org.xmldb.api.base.Database;
import org.xmldb.api.base.Resource;
import org.xmldb.api.base.ResourceIterator;
import org.xmldb.api.base.ResourceSet;
import org.xmldb.api.modules.XMLResource;


/**
 *
 * @author Petra
 */
public class FindXml {
protected static String driver = "org.exist.xmldb.DatabaseImpl";

	protected static String URI = "xmldb:exist://localhost:8080/exist/xmlrpc/";
        private static String collection = "biographies";

	public static String findCV(Integer userID, String selectedCV) throws Exception {
		// inicializace databaze
		Class<?> cl = Class.forName(driver);
		Database database = (Database) cl.newInstance();
                
		DatabaseManager.registerDatabase(database);

		// ziskani kolekce
		Collection col = DatabaseManager.getCollection(URI + collection);
		col.setProperty(OutputKeys.INDENT, "yes");
		col.setProperty(EXistOutputKeys.EXPAND_XINCLUDES, "no");
                col.setProperty(EXistOutputKeys.PROCESS_XSL_PI, "yes");
                System.out.println("Selected CV now = " + selectedCV);
                String xquery = "for $x in /cvFile[@date = \"" + selectedCV + "\" and @userID = " + userID + "]\n" +
                "return $x/cv";
                
                XQueryService service = (XQueryService) col.getService("XQueryService", "1.0");
                service.setProperty("indent", "yes");
                ResourceSet result = service.query(xquery);
                ResourceIterator i = result.getIterator();
                do {
                    Resource r = i.nextResource();
                    String value = (String)r.getContent();
                    return value;
                } while (i.hasMoreResources());
	}
        
        public static List<String> findCvDates(Integer userID) throws Exception {
            // inicializace databaze
            Class<?> cl = Class.forName(driver);
            Database database = (Database) cl.newInstance();

            DatabaseManager.registerDatabase(database);

            // ziskani kolekce
            Collection col = DatabaseManager.getCollection(URI + collection);
            col.setProperty(OutputKeys.INDENT, "yes");
            col.setProperty(EXistOutputKeys.EXPAND_XINCLUDES, "no");
            col.setProperty(EXistOutputKeys.PROCESS_XSL_PI, "yes");

            String xquery = "for $x in /cvFile[@userID = " + userID + "]\n" +
                "return data($x/@date)";

            XQueryService service = (XQueryService) col.getService("XQueryService", "1.0");
            service.setProperty("indent", "yes");
            ResourceSet result = service.query(xquery);
            ResourceIterator i = result.getIterator();
            List<String> values = new ArrayList<String>();
            do {
                Resource r = i.nextResource();
                values.add((String)r.getContent());
            } while (i.hasMoreResources());
            return values;
	}
}
